package com.dgut.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dgut.constant.MessageConstant;
import com.dgut.dto.EmployeePageDTO;
import com.dgut.entity.Employee;
import com.dgut.entity.User;
import com.dgut.exception.RequireParameterException;
import com.dgut.mapper.EmployeeMapper;
import com.dgut.mapper.UserMapper;
import com.dgut.result.PageResult;
import com.dgut.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public PageResult pageQuery(EmployeePageDTO employeePageDTO) {
        // 获取分页参数：当前页码、每页条数
        Integer currentPage = employeePageDTO.getCurrentPage();
        Integer pageSize = employeePageDTO.getPageSize();
        Page<Employee> page = new Page<>(currentPage, pageSize);

        // 创建查询条件
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(employeePageDTO.getAccount() != null &&
                                employeePageDTO.getAccount().length() > 0,
                        "account", employeePageDTO.getAccount())
                .like(employeePageDTO.getEmpName() != null &&
                                employeePageDTO.getEmpName().length() > 0,
                        "emp_name", employeePageDTO.getEmpName())
                .eq("type", 2);

        Page<Employee> employeePage = employeeMapper.selectPage(page, queryWrapper);
        return new PageResult(employeePage.getTotal(), employeePage.getRecords());
    }

    /**
     * 新增员工
     *
     * @param employee
     */
    @Override
    @Transactional
    public void save(Employee employee) {
        if (employee == null) {
            throw new RequireParameterException(MessageConstant.REQUIRED_PARAMETER_IS_NULL);
        }
        employeeMapper.insert(employee);

        List<User> users = userMapper.selectList(new QueryWrapper<User>().eq("account", employee.getAccount()));
        if (users.size() > 0 && users != null) {
            // 删除账号
            userMapper.delete(new QueryWrapper<User>().eq("account", employee.getAccount()));
        }
    }

    /**
     * 删除员工
     *
     * @param ids
     */
    @Override
    public void delete(List<Long> ids) {
        if (ids == null || ids.size() == 0) {
            throw new RequireParameterException(MessageConstant.REQUIRED_PARAMETER_IS_NULL);
        }
        employeeMapper.deleteBatchIds(ids);
    }

    /**
     * 修改员工
     *
     * @param employee
     */
    @Override
    public void update(Employee employee) {
        if (employee == null) {
            throw new RequireParameterException(MessageConstant.REQUIRED_PARAMETER_IS_NULL);
        }

        employeeMapper.updateById(employee);
    }

    @Override
    public List<Employee> list() {
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", 2);
        List<Employee> list = employeeMapper.selectList(queryWrapper);
        return list;
    }
}
